﻿using SharPy.GameSettings;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace SharPy.GameObjects
{
    public class clGame
    {
        public StorageFolder RootFolder;
        public StorageFolder GameFolder;

        public StorageFile ScriptFile;
        public StorageFile ConfigFile;

        public string Name;
        public string Path;

        public int LineNoHolder;

        public clGame() { this.LineNoHolder = 0; } // Make sure to await game registration after using this

        public async Task<int> RegisterGame (StorageFolder p)
        {
            //TODO: check if obsolete
            
            this.RootFolder = p;
            try
            {
                this.GameFolder = await p.GetFolderAsync("game");
            }
            catch (System.IO.FileNotFoundException)
            {
                if (p.Name == "game")
                    this.GameFolder = p;
                else
                    SharPy.Interface.clDialogs.Error("Cannot locate game directory");
            }
            try
            {
                this.ScriptFile = await this.GameFolder.GetFileAsync("script.rpy");            
            }
            catch (System.IO.FileNotFoundException)
            {
                SharPy.Interface.clDialogs.Error("Cannot locate script file");
            }
            try
            {
                this.ConfigFile = await this.GameFolder.GetFileAsync("options.rpy");
            }
            catch (System.IO.FileNotFoundException)
            {
            }

            //Check if obsolete
            SharPy.Parser.clText.gameFolder = this.GameFolder;

            clSettings.saveFolder(this.RootFolder);

            this.Name = p.Name;
            this.Path = p.Path;
            return 1;
        }
        public static void FlushGameInfo()
        { 

        }
    }
}
